﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Threading;
using HAF.FoundationLib;
using WareDealer.Helper;
using WareDealer.Mode;
using WareDealer.Works;

namespace WareDealer
{
    /// <summary>
    /// 调度器
    /// </summary>
    [DisplayName("任务调度器")]
    public sealed class Dispatcher : IProcessOut
    {
        #region constructor
        private Dispatcher()
        {
        }

        private static Dispatcher _dispatcher;
        /// <summary>
        /// 获取调度单实例
        /// </summary>
        /// <returns></returns>
        public static Dispatcher GetInstance()
        {
            return _dispatcher ?? (_dispatcher = new Dispatcher());
        }
        #endregion constructor

        #region Interface release
        /// <summary>
        /// 进度条编号/名称
        /// </summary>
        public List<string> ProcessBarIDs { get; set; }
        /// <summary>
        /// 初始化线程
        /// </summary>
        /// <remarks>用于进度条准备工作</remarks>
        public Action<string, int> InitProcess { get; set; }
        /// <summary>
        /// 操作进度条
        /// </summary>
        public Action<string, int> ShowStep { get; set; }
        /// <summary>
        /// 显示消息
        /// </summary>
        public Action<string> ShowMessage { get; set; }
        /// <summary>
        /// 线程完成
        /// </summary>
        public Action<string, bool> EndProcess { get; set; }
        #endregion Interface release

        #region Release Private Method

        private void InitAutoProcess(string processid, int iLength)
        {
            //Faster.XLogInfo(processid);
            if (!string.IsNullOrEmpty(processid) && InitProcess != null)
            {
                InitProcess(processid, iLength);
            }
        }

        private void ShowAutoStep(string processid, int iStep)
        {
            if (!string.IsNullOrEmpty(processid) && ShowStep != null)
            {
                ShowStep(processid, iStep);
            }
        }

        private void EndAutoProcess(string processid, bool bRtn)
        {
            if (!string.IsNullOrEmpty(processid) && EndProcess != null)
            {
                EndProcess(processid, bRtn);
            }
        }

        private void ShowAutoMessage(string msg)
        {
            if (ShowMessage != null)
            {
                ShowMessage(msg);
            }
        }

        private int _iThreadEndTime = 1;

        private void GatherTaskEnd()
        {
            ShowAutoStep("main", _iThreadEndTime);
            _iThreadEndTime++;
        }

        #endregion Release Private Method
       
        /// <summary>
        /// 运行中的任务集
        /// </summary>
        private readonly List<GatherTask> _runTasks = new List<GatherTask>();
        /// <summary>
        /// 当前运行线程数
        /// </summary>
        private int _runThreadCount = 1;
        /// <summary>
        /// 工作项计数器
        /// </summary>
        private int _runWorkItemCount = 1;
        /// <summary>
        /// 线程最大数量
        /// </summary>
        private readonly int _threadMaxCount = 4;
        /// <summary>
        /// 分离任务执行条件
        /// </summary>
        /// <remarks>超过该值进行任务分离操作</remarks>
        private const int iSplitCondition = 1000;
        /// <summary>
        /// 单次采集任务商品数量
        /// </summary>
        private const int iGetSingleLength = 100;

        /// <summary>
        /// 启动采集任务
        /// </summary>
        public void StartGatherTask(List<ProductInfo> pidList)
        {
            DbHelper.GetInstance().WareWrongHisPriceDel();
            _runTasks.Clear();
            _runWorkItemCount = 1;
            _runThreadCount = 1;
            _iThreadEndTime = 1;
            TaskSplit(pidList);
            DispathTask();
        }

        #region CreateGatherTask
        /// <summary>
        /// 创建采集任务
        /// </summary>
        /// <param name="tWares">商品集合</param>
        /// <param name="tType">采集任务类型</param>
        /// <param name="i">进度条编号</param>
        /// <returns></returns>
        private GatherTask CreateGatherTask(List<ProductInfo> tWares, EnumGatherTaskType tType, int i)
        {
            var runTask = new GatherTask()
            {
                Num = i+1,
                WareInfos = tWares,
                TaskStatu = EnumGatherStatu.Initialize,
                TaskType = tType
            };
            if (SysParams.UpdateType == EnumUpdateType.Manual)
            {
                runTask.ProcessBarID = ProcessBarIDs != null && ProcessBarIDs.Count > 0 ? ProcessBarIDs[i] : "";
            }
            else
            {
                runTask.ProcessBarID = "main";
            }
            return runTask;
        }
        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="tWares"></param>
        /// <param name="i"></param>
        private void CreateTask(List<ProductInfo> tWares, int i)
        {
            //线程计数器
            int iProcessNum = i;
            //创建价格采集任务
            if (SysParams.AllowGetPrice)
            {
                _runTasks.Add(CreateGatherTask(tWares, EnumGatherTaskType.Price, iProcessNum));
            }
            //创建库存采集任务
            if (SysParams.AllowGetStock)
            {
                iProcessNum = SysParams.AllowGetPrice && SysParams.AllowGetStock
                    ? i + SysParams.GatherMaxCount
                    : i;
                _runTasks.Add(CreateGatherTask(tWares, EnumGatherTaskType.Stock, iProcessNum));
            }
            //创建广告采集任务
            if (SysParams.AllowGetCoupon)
            {
                iProcessNum = SysParams.AllowGetPrice && SysParams.AllowGetStock && SysParams.AllowGetCoupon
                    ? i + SysParams.GatherMaxCount + SysParams.GatherMaxCount
                    : SysParams.AllowGetStock && SysParams.AllowGetCoupon ? i + SysParams.GatherMaxCount : i;
                _runTasks.Add(CreateGatherTask(tWares, EnumGatherTaskType.SubTitle, iProcessNum));
            }
            //创建优惠采集任务
            if (SysParams.AllowGetPromo)
            {
                iProcessNum = SysParams.AllowGetPrice && SysParams.AllowGetStock && SysParams.AllowGetCoupon
                    ? i + SysParams.GatherMaxCount + SysParams.GatherMaxCount
                    : SysParams.AllowGetStock && SysParams.AllowGetCoupon ? i + SysParams.GatherMaxCount : i;
                _runTasks.Add(CreateGatherTask(tWares, EnumGatherTaskType.Coupon, iProcessNum));
            }
        }

        /// <summary>
        /// 按线程最大数分离任务数据
        /// </summary>
        /// <param name="wares">待分离商品</param>
        private void TaskSplit(List<ProductInfo> wares)
        {
            if (wares == null || wares.Count <= 0) return;
            ShowAutoMessage("按线程最大数分离任务数据开始");
            //商品采集数量大于分离任务条件，进行任务分离
            if (wares.Count > iSplitCondition)
            {
                //根据商品总量确定分离后每个线程商品个数
                float taskCount = (float)wares.Count / _threadMaxCount;
                int taskSplitCount = (int)wares.Count / _threadMaxCount;
                if (taskSplitCount < taskCount)
                {
                    taskSplitCount++;
                }
                //当前分离商品数量
                int splitCount = 1;
                int iCount = 0;
                //商品分离页数
                int iSplitPage = 0;
                var splitWareList = new List<ProductInfo>();
                foreach (var item in wares)
                {
                    splitWareList.Add(item);
                    splitCount++;
                    iCount++;
                    if (splitCount > taskSplitCount || (iCount == wares.Count && splitCount <= taskSplitCount))
                    {
                        CreateTask(splitWareList, iSplitPage);
                        if (iCount >= wares.Count)
                        {
                            break;
                        }
                        iSplitPage++;
                        splitCount = 0;
                        splitWareList = new List<ProductInfo>();
                    }
                    
                    
                }
            }
            else
            {
                CreateTask(wares, 0);
                //_runTasks.Add(new GatherTask() { WareInfos = wares, TaskStatu = EnumGatherTaskStatu.Initialize, ProcessBarID = ProcessBarIDs != null && ProcessBarIDs.Count > 0 ? ProcessBarIDs[0] : "" });
            }
            ShowAutoMessage("按线程最大数分离任务数据完成");
        }
        #endregion CreateTask

        /// <summary>
        /// 任务调度
        /// </summary>
        private void DispathTask()
        {
            if (_runTasks.Count <= 0) return;

            _runThreadCount = 1;
            foreach (var item in _runTasks)
            {
                RunThread(item);
                _runThreadCount++;
            }
            if (SysParams.UpdateType == EnumUpdateType.Automatic)
            {
                InitAutoProcess("main", _runThreadCount);
            }
            while (true)
            {
                List<GatherTask> execTasks = _runTasks.FindAll(t => t.TaskStatu != EnumGatherStatu.End);
                if (execTasks.Count == 0)
                {
                    SaveToDb();
                    break;
                }
            }
        }

        /// <summary>
        /// 启动新线程
        /// </summary>
        /// <param name="rTask"></param>
        private void RunThread(GatherTask rTask)
        {
            if (rTask == null) return;
            _warePries4Batch.Clear();
            _wareStock4Batch.Clear();
            _wareAds4Batch.Clear();
            _warePromos4Batch.Clear();

            rTask.TaskStatu = EnumGatherStatu.Begin;
            ShowAutoMessage("启动子线程"+rTask.TaskID);
            
            //Thread runThread = new Thread(delegate() { GatherBatchThread(rTask.WareInfos, rTask.TaskID); }) { Name = "GatherTask" + _runThreadCount, IsBackground = true };
            Thread runThread = new Thread(delegate() { GatherBatchThread(rTask); }) { Name = "GatherTask" + _runThreadCount, IsBackground = true };
            runThread.Start();
        }

        /// <summary>
        /// 批量更新价格线程
        /// </summary>
        /// <remarks>默认一个批次100个商品</remarks>
        [DisplayName("批量采集线程")]
        private void GatherBatchThread(List<ProductInfo> wares, string taskId)
        {
            if (wares != null && wares.Count > 0)
            {
                try
                {
                    ShowAutoMessage(string.Format("商品采集子线程{0}开始，本批次采集总计{1}个商品", Thread.CurrentThread.Name, wares.Count));
                    Faster.XLogInfo(string.Format("商品采集子线程{0}开始，本批次采集总计{1}个商品", Thread.CurrentThread.Name, wares.Count));
                    GatherTask rTask = _runTasks.Find(t => t.TaskID == taskId);
                    if (rTask == null)
                    {
                        return;
                    }

                    rTask.TaskStatu = EnumGatherStatu.Exec;

                    int i = 0;
                    int j = 0;
                    List<ProductInfo> subList = new List<ProductInfo>();
                    //1、商品总数量不足100的判断
                    //2、商品按100数量进行分页处理，当剩余商品数量不足100时的判断
                    int iPage = wares.Count / iGetSingleLength; //
                    int iRemainder = wares.Count % iGetSingleLength; //余数
                    int iCurPage = 1;
                    InitAutoProcess(rTask.ProcessBarID, iPage);
                    foreach (var item in wares)
                    {
                        //Faster.XLogInfo(string.Format("当前第{0}个商品", i));
                        i++;
                        j++;
                        subList.Add(item);

                        if ((wares.Count < iGetSingleLength && i == wares.Count) || j == iGetSingleLength
                            || (j == iRemainder && (i > (iPage * iGetSingleLength)) && (i <= (iPage * iGetSingleLength + iRemainder))))
                        {
                            Faster.XLogInfo(string.Format("商品采集线程{0}执行第{1}页", Thread.CurrentThread.Name, iCurPage));
                            ShowAutoMessage(string.Format("商品采集线程{0}执行第{1}页", Thread.CurrentThread.Name, iCurPage));
                            RunWorkItem(subList, rTask.TaskType, rTask.Num);
                            subList.Clear();
                            j = 0;
                            iCurPage++;
                        }

                        ShowAutoStep(rTask.ProcessBarID, iCurPage);
                    }
                    EndAutoProcess(rTask.ProcessBarID, true);
                    rTask.TaskStatu = EnumGatherStatu.End;
                    ShowAutoMessage(string.Format("商品采集线程{0}结束", Thread.CurrentThread.Name));
                    Faster.XLogInfo(string.Format("商品采集线程{0}结束", Thread.CurrentThread.Name));
                }
                catch (Exception ex)
                {
                    Faster.XLogErr(ex.Message);
                }
                
            }
        }
        /// <summary>
        /// 批量采集线程
        /// </summary>
        /// <param name="rTask">当前任务</param>
        [DisplayName("批量采集线程")]
        private void GatherBatchThread(GatherTask rTask)
        {
            if (rTask == null)
            {
                return;
            }
            if (rTask.WareInfos != null && rTask.WareInfos.Count > 0)
            {
                try
                {
                    ShowAutoMessage(string.Format("商品{0}采集子线程{1}开始，本批次采集总计{2}个商品", rTask.TaskTypeName, Thread.CurrentThread.Name, rTask.WareInfos.Count));
                    Faster.XLogInfo(string.Format("商品{0}采集子线程{1}开始，本批次采集总计{2}个商品", rTask.TaskTypeName, Thread.CurrentThread.Name, rTask.WareInfos.Count));

                    rTask.TaskStatu = EnumGatherStatu.Exec;

                    int i = 0;
                    int j = 0;
                    List<ProductInfo> subList = new List<ProductInfo>();
                    //1、商品总数量不足100的判断
                    //2、商品按100数量进行分页处理，当剩余商品数量不足100时的判断
                    int iPage = rTask.WareInfos.Count / iGetSingleLength; //
                    int iRemainder = rTask.WareInfos.Count % iGetSingleLength; //余数
                    int iCurPage = 1;
                    if (SysParams.UpdateType == EnumUpdateType.Manual) InitAutoProcess(rTask.ProcessBarID, iPage);
                    
                    foreach (var item in rTask.WareInfos)
                    {
                        //Faster.XLogInfo(string.Format("当前第{0}个商品", i));
                        i++;
                        j++;
                        subList.Add(item);

                        if ((rTask.WareInfos.Count < iGetSingleLength && i == rTask.WareInfos.Count) || j == iGetSingleLength
                            || (j == iRemainder && (i > (iPage * iGetSingleLength)) && (i <= (iPage * iGetSingleLength + iRemainder))))
                        {
                            Faster.XLogInfo(string.Format("商品{0}采集子线程{1}执行第{2}页", rTask.TaskTypeName, Thread.CurrentThread.Name, iCurPage));
                            ShowAutoMessage(string.Format("商品{0}采集子线程{1}执行第{2}页", rTask.TaskTypeName, Thread.CurrentThread.Name, iCurPage));
                            RunWorkItem(subList, rTask.TaskType, rTask.Num);
                            subList.Clear();
                            j = 0;
                            iCurPage++;
                        }

                        if (SysParams.UpdateType == EnumUpdateType.Manual) ShowAutoStep(rTask.ProcessBarID, iCurPage);
                    }
                    rTask.TaskStatu = EnumGatherStatu.End;
                    if (SysParams.UpdateType == EnumUpdateType.Manual)
                    {
                        EndAutoProcess(rTask.ProcessBarID, true);
                    }
                    else
                    {
                        GatherTaskEnd();
                    }
                    
                    ShowAutoMessage(string.Format("商品{0}采集子线程{1}结束", rTask.TaskTypeName, Thread.CurrentThread.Name));
                    Faster.XLogInfo(string.Format("商品{0}采集子线程{1}结束", rTask.TaskTypeName, Thread.CurrentThread.Name));
                }
                catch (Exception ex)
                {
                    Faster.XLogErr(ex.Message);
                }

            }
        }
        /// <summary>
        /// 批采集价格集合
        /// </summary>
        private readonly List<JdWarePriceCache> _warePries4Batch = new List<JdWarePriceCache>();
        /// <summary>
        /// 批采集库存集合
        /// </summary>
        private readonly List<WareStock> _wareStock4Batch = new List<WareStock>();
        /// <summary>
        /// 批采集广告集合
        /// </summary>
        private readonly List<AdMessageTag> _wareAds4Batch = new List<AdMessageTag>();
        /// <summary>
        /// 批采集优惠集合
        /// </summary>
        private readonly List<PrivilegeTag> _warePromos4Batch = new List<PrivilegeTag>();
        private object _lockObj = new object();
        /// <summary>
        /// 执行工作项
        /// </summary>
        /// <param name="pidList">商品列表</param>
        /// <param name="tType">任务类型</param>
        private void RunWorkItem(List<ProductInfo> pidList, EnumGatherTaskType tType, int taskNum)
        {
            
            IWorkItem runItem = null;
            switch (tType)
            {
                
                case EnumGatherTaskType.Stock:
                    runItem = new JdStockWork()
                    {
                        ID = _runWorkItemCount,
                        Name = string.Format("GarherStock{0}-{1}", taskNum, _runWorkItemCount),
                        Priority = 0,
                        Time = 0,
                        WorkItems = pidList
                    };
                    break;
                case EnumGatherTaskType.SubTitle:
                    runItem = new JdSubHeadWork()
                    {
                        ID = _runWorkItemCount,
                        Name = string.Format("GarherSubTitle{0}-{1}", taskNum, _runWorkItemCount),
                        Priority = 0,
                        Time = 0,
                        WorkItems = pidList
                    };
                    break;
                case EnumGatherTaskType.Coupon:
                    runItem = new JdCouponWork()
                    {
                        ID = _runWorkItemCount,
                        Name = string.Format("GarherCoupon{0}-{1}", taskNum, _runWorkItemCount),
                        Priority = 0,
                        Time = 0,
                        WorkItems = pidList
                    };
                    break;
                default:
                    runItem = new JdPriceWork()
                    {
                        ID = _runWorkItemCount,
                        Name = string.Format("GarherPrice{0}-{1}", taskNum, _runWorkItemCount),
                        Priority = 0,
                        Time = 0,
                        WorkItems = pidList
                    };
                    break;
            }
            _runWorkItemCount++;

            runItem.ExecItem();

            while (true)
            {
                if (runItem.Statu == EnumGatherStatu.End)
                {
                    lock (_lockObj)
                    {
                        object rtnObj = runItem.ResultItems;
                        //商品价格
                        var listPrice = rtnObj as List<JdWarePriceCache>;
                        if (listPrice != null)
                        {
                            _warePries4Batch.AddRange(listPrice);
                        }
                        //商品库存
                        var listStock = rtnObj as List<WareStock>;
                        if (listStock != null)
                        {
                            _wareStock4Batch.AddRange(listStock);
                        }
                        //商品促销
                        var listAd = rtnObj as List<AdMessageTag>;
                        if (listAd != null)
                        {
                            _wareAds4Batch.AddRange(listAd);
                        }
                        //商品优惠
                        var listProm = rtnObj as List<PrivilegeTag>;
                        if (listProm != null)
                        {
                            _warePromos4Batch.AddRange(listProm);
                        }
                        break;
                    }
                    
                }
                
            }

        }
        
        /// <summary>
        /// 获取当次采集最小值
        /// </summary>
        /// <param name="price1"></param>
        /// <param name="price2"></param>
        /// <param name="price3"></param>
        /// <param name="price4"></param>
        /// <returns></returns>
        private JdWarePrice GetMinPriceSrc(double price1, double price2, double price3, double price4)
        {
            JdWarePrice tmpJDPrice = new JdWarePrice();
            tmpJDPrice.p = price1.ToString();
            tmpJDPrice.src = "京东";
            //加入double数组
            double[] listPrice = new double[4];
            listPrice[0] = price1;
            listPrice[1] = price2;
            listPrice[2] = price3;
            listPrice[3] = price4;

            //存储临时的需要冒泡的值
            double tempPrice = listPrice.Max();

            for (int i = 0; i < listPrice.Length; i++)
            {
                //当值为0或-1时，取最大值代替
                if (listPrice[i] == 0 || listPrice[i] == -1)
                {
                    listPrice[i] = tempPrice;
                }
            }

            //tempPrice = listPrice.Min();

            tempPrice = listPrice[0];
            //价格索引，用于判断是哪个平台价格最低
            int tempIndex = 0;
            //从数组的第一个值遍历到倒数第二个值
            for (int i = 1; i < listPrice.Length; i++)
            {
                if (listPrice[i] < tempPrice)
                {
                    tempPrice = listPrice[i];
                    tempIndex = i;
                }
                ////从比i大1的值开始遍历到结束
                ////这里比较的总是比i大的值，因为之前的值已经冒泡完成
                //for (int j = i + 1; j < listPrice.Length; j++)
                //{
                //    //如果前一个值大于后一个值，他们交换位置
                //    if (listPrice[i] > listPrice[j])
                //    {
                //        //交换位置
                //        tempPrice = listPrice[i];
                //        listPrice[i] = listPrice[j];
                //        listPrice[j] = tempPrice;
                //    }
                //}
            }
            switch (tempIndex)
            {
                case 0:
                    tmpJDPrice.src = "京东";
                    break;
                case 1:
                    tmpJDPrice.src = "手机";
                    break;
                case 2:
                    tmpJDPrice.src = "QQ";
                    break;
                case 3:
                    tmpJDPrice.src = "微信";
                    break;
            }
            tmpJDPrice.p = tempPrice.ToString(CultureInfo.InvariantCulture);

            return tmpJDPrice;
        }
        /// <summary>
        /// 获取当次采集最小值
        /// </summary>
        /// <param name="jdWarePrice"></param>
        private void GetMinPriceSrc(JdWarePriceCache jdWarePrice)
        {
            if (jdWarePrice == null) return;
            jdWarePrice.MinPrice = 0;
            jdWarePrice.MinPriceSrc = "京东";
            //加入double数组
            double[] listPrice = new double[4];
            listPrice[0] = jdWarePrice.WebPrice;
            listPrice[1] = jdWarePrice.AppPrice;
            listPrice[2] = jdWarePrice.QqPrice;
            listPrice[3] = jdWarePrice.WxPrice;

            //存储临时的需要冒泡的值
            double tempPrice = listPrice.Max();

            for (int i = 0; i < listPrice.Length; i++)
            {
                //当值为0或-1时，取最大值代替
                if (listPrice[i] == 0 || listPrice[i] == -1)
                {
                    listPrice[i] = tempPrice;
                }
            }

            tempPrice = listPrice[0];
            //价格索引，用于判断是哪个平台价格最低
            int tempIndex = 0;
            //从数组的第一个值遍历到倒数第二个值
            for (int i = 1; i < listPrice.Length; i++)
            {
                if (listPrice[i] < tempPrice)
                {
                    tempPrice = listPrice[i];
                    tempIndex = i;
                }
                ////从比i大1的值开始遍历到结束
                ////这里比较的总是比i大的值，因为之前的值已经冒泡完成
                //for (int j = i + 1; j < listPrice.Length; j++)
                //{
                //    //如果前一个值大于后一个值，他们交换位置
                //    if (listPrice[i] > listPrice[j])
                //    {
                //        //交换位置
                //        tempPrice = listPrice[i];
                //        listPrice[i] = listPrice[j];
                //        listPrice[j] = tempPrice;
                //    }
                //}
            }
            switch (tempIndex)
            {
                case 0:
                    jdWarePrice.MinPriceSrc = "京东";
                    break;
                case 1:
                    jdWarePrice.MinPriceSrc = "手机";
                    break;
                case 2:
                    jdWarePrice.MinPriceSrc = "QQ";
                    break;
                case 3:
                    jdWarePrice.MinPriceSrc = "微信";
                    break;
            }
            jdWarePrice.MinPrice = tempPrice;

        }

        #region 商品数据入库
        /// <summary>
        /// 数据入库
        /// </summary>
        [DisplayName("商品数据入库")]
        private void SaveToDb()
        {
            Faster.XLogInfo("保存批量采集数据入库开始");
            ShowAutoMessage("保存批量采集数据入库开始");
            string procId = "";
            int index = 0;
            if (SysParams.UpdateType == EnumUpdateType.Manual)
            {
                index = ProcessBarIDs != null && ProcessBarIDs.Count > 0 ? ProcessBarIDs.Count - 1 : -1;

                if (index != -1)
                {
                    procId = ProcessBarIDs[index];
                }

                index = 0;
                InitAutoProcess(procId, 3);
                index++;
            }
            SaveWaresPrice();
            if (SysParams.UpdateType == EnumUpdateType.Manual)
            {
                ShowAutoStep(procId, index);
                index++;
            }
            SaveWareStock();
            if (SysParams.UpdateType == EnumUpdateType.Manual)
            {
                ShowAutoStep(procId, index);
                index++;
            }
            SaveWareSubHead();
            SaveWareCoupon();
            if (SysParams.UpdateType == EnumUpdateType.Manual)
            {
                ShowAutoStep(procId, index);
                EndAutoProcess(procId, true);
            }
            else
            {
                EndAutoProcess("main", true);
            }
            Thread.Sleep(100);
            ShowAutoMessage("保存批量采集数据入库结束");
            Faster.XLogInfo("保存批量采集数据入库结束");
        }
        /// <summary>
        /// 价格信息入库
        /// </summary>
        private void SaveWaresPrice()
        {
            if (_warePries4Batch.Count > 0)
            {
                //To-Do: 算法优化，进行批事务处理，而不是一个个的都进行事务，解决入库瓶颈问题
                //foreach (var item in _warePries4Batch)
                //{
                //    //获取商品当批次最低价
                //    GetMinPriceSrc(item);

                //}
                DbHelper.GetInstance().WarePriceBatchUpdate(_warePries4Batch);
                DbHelper.GetInstance().WareHisPriceBatchInsert(_warePries4Batch);
                //DbHelper.GetInstance().UpdateMinMaxPriceBatch(_warePries4Batch);
            }
            
        }
        /// <summary>
        /// 库存信息入库
        /// </summary>
        private void SaveWareStock()
        {
            if (_wareStock4Batch.Count > 0)
            {
                DbHelper.GetInstance().WareRepositoryBatchUpdate(_wareStock4Batch);
            }
        }
        /// <summary>
        /// 促销信息入库
        /// </summary>
        private void SaveWareSubHead()
        {
            if (_wareAds4Batch.Count > 0)
            {
                DbHelper.GetInstance().WareAdMessageBatchUpdate(_wareAds4Batch);
            }
        }
        /// <summary>
        /// 优惠信息入库
        /// </summary>
        private void SaveWareCoupon()
        {
            if (_warePromos4Batch.Count > 0)
            {
                DbHelper.GetInstance().WarePromotionBatchUpdate(_warePromos4Batch);
            }
        }
        #endregion 商品数据入库
    }
}
